<template>
	<view>
		<!-- <view v-if="isCanUse"> -->
			<view>
			<view>
				<view class='header'>
					<image src='../../../static/Mine/wx.png'></image>
				</view>
				<view class='content'>
					<view>申请获取以下权限</view>
					<text>获得你的公开信息(昵称，头像、地区等)</text>
				</view>

				<button class='bottom' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN"
					@click="newWxLogin">
					授权登录
				</button>
				<button class='bottom2' >暂不登录</button>
			</view>
		</view>

		<!-- <view v-if="isCanUse == false">
			<view class="mine-hader">
				<view class="avater-img">
					<image :src="info.avatar" v-if="info.avatar"></image>
					<image src="../../static/Mine/5.png" v-else></image>
				</view>
				<text v-if="info.username">{{info.username}}</text>
				<text @click="chanGe" v-else>点击快捷登录</text>
			</view>
		</view> -->

	</view>
</template>

<script>
	import {
		third
	} from '../../../api/mine.js';
	export default {
		data() {
			return {
				// isCanUse: false,
				req: {
					platform: 'wechat',
					code: '',
					rawData: ''
				},
				info: {}
			}
		},
		onShow() {
			this.wechat();
		},
		methods: {
			oauth(value) {
				var th_is = this;
				uni.login({
					provider: value,
					success: function(loginRes) {
						th_is.req.code = loginRes.code;
						th_is.login();
					},
					fail() {
						uni.showToast({
							icon: 'none',
							title: '登录失败，请稍后重试！'
						})
					}
				});
			},
			newWxLogin() {
				var th_is = this;
				uni.getUserProfile({
					desc: '登录',
					success: (res) => {
						console.log(res);
						// 获取用户信息
						th_is.req.rawData = res.rawData;
						th_is.oauth('userinfo');
					},
					fail: (err) => {
						// this.isCanUse = false;
						uni.showTabBar();
						console.log(err);
					}
				})
			},

			chanGe() {
				// this.isCanUse = true;
				uni.hideTabBar();
			},
			linkurl(e) {
				uni.navigateTo({
					url: e
				});
			},
			async login() {
				console.log(this.req);
				const res = await third(this.req);
						console.log(res)
						// console.log('res.userinfo',res.data.thirdinfo.userinfo)
						if (res.code == 200) {
							uni.switchTab({
								url: '/pages/TabBar/Mine/Mine'
							})
							
							// if ((res.data.userinfo).mobile !== '') {
							// 	uni.switchTab({
							// 		url: '/pages/TabBar/Home/Home'
							// 	})
							// } 
					// else {
					// 			uni.navigateTo({
					// 				url: '/pages/Mine/changeBindingMobilePhone?type=' +
					// 					1,
					// 				fail(err) {
					// 					console.log(err)
					
					// 				}
					// 			})
					// 		}
							uni.showToast({
								title: '微信登录成功'
							})
							uni.setStorageSync('userinfo', res.data.thirdinfo.userinfo);
							uni.setStorageSync('token', res.data.userinfo
								.token)
							this.token = res.data.userinfo.token
							// this.isCanUse = false;
							this.wechat();
						} else {
							uni.showToast({
								title: '微信登录失败'
							})
						}
			},
			wechat() {
				if(uni.getStorageSync('userinfo')){
					this.info = uni.getStorageSync('userinfo');
					console.log('info',info)
					
				}else{
					uni.showToast({
						icon: 'none',
						title: '请先登录！'
					});
				}
			}
		}
	}
</script>

<style scoped lang="less">
	page {
		background-color: #f0f2f5;
	}

	button::after {
		border: none
	}

	.mine-hader {
		width: 100%;
		height: 450rpx;
		background-color: #007AFF;
		text-align: center;
		opacity: 0.72;

		.avater-img {
			padding-top: 40rpx;
		}

		image {
			width: 230rpx;
			height: 230rpx;
			border-radius: 50%;
			display: block;
			margin: 0px auto;
			margin-top: 50rpx;
		}

		text {
			display: block;
			color: #FFFFFF;
			font-size: 35rpx;
			font-weight: 500;
			padding-top: 20rpx;
		}
	}

	.mine-list {
		width: 100%;

		.u-list {
			width: 86%;
			margin: 0px auto;
			height: 120rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-left: 20rpx;

			.u-flex {
				display: flex;
				align-items: center;
			}

			image {
				width: 55rpx;
				height: 60rpx;
			}

			text {
				font-size: 30rpx;
				padding-left: 30rpx;
				color: #a3a4a4;
			}
		}
	}

	.header {
		margin-top: 50rpx;
		// margin: 90rpx 0 90rpx 50rpx;
		border-bottom: 1px solid #ccc;
		text-align: center;		
		line-height: 350rpx;
	}

	.content {
		margin-top: 50rpx;
		margin-left: 50rpx;
		margin-bottom: 50rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
	}
	.bottom2 {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
		background-color: #E2E8F0;
	}
</style>
